﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

using ReactiveLang;

namespace SolarSystem
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }

        [Reactive(AutoRun = true)]
        void GoGoPlanets()
        {
            double angle = DateTime.Now.TimeOfDay.TotalSeconds * 2 * 360 / 60 * Math.PI / 180;

            var sunCoords = new Point(Canvas.GetLeft(sun) + sun.Width / 2, Canvas.GetTop(sun) + sun.Height / 2);

            var earthCoords = sunCoords + new Vector
            (
                earthRadius.Value * Math.Cos(angle),
                earthRadius.Value * Math.Sin(angle)
            );

            Canvas.SetLeft(earth, earthCoords.X - earth.Width / 2);
            Canvas.SetTop(earth, earthCoords.Y - earth.Height / 2);

            moon.Visibility = showMoon.IsChecked == true ? Visibility.Visible : Visibility.Collapsed;
            Canvas.SetLeft(moon, earthCoords.X + moonRadius.Value * Math.Cos(angle * 12) - moon.Width / 2);
            Canvas.SetTop(moon, earthCoords.Y + moonRadius.Value * Math.Sin(angle * 12) - moon.Height / 2);
        }

        private void Canvas_MouseDown(object sender, MouseButtonEventArgs e)
        {
            var xy = Mouse.GetPosition((IInputElement)sender);
            Canvas.SetLeft(sun, xy.X - sun.Width / 2);
            Canvas.SetTop(sun, xy.Y - sun.Height / 2);
        }
    }
}
